﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class ThemTacGia : Form
    {
        public ThemTacGia()
        {
            InitializeComponent();
        }
        private void AnHienButtonSave()
        {
            if (dgvTacGia.Rows.Count - 1 != 0)
            {
                btnSave.Enabled = true;
            }
            else
            {
                btnSave.Enabled = false;
            }
        }
        private void AnHienButtonXoa()
        {
            if (dgvTacGia.Rows.Count - 1 != 0)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        private void SetUpDataGridViewTacGia()
        {
            dgvTacGia.BackgroundColor = Color.White;
            dgvTacGia.ColumnCount = 2;
            dgvTacGia.Columns[0].Name = "STT";
            dgvTacGia.Columns[1].Name = "Tên Tác Giả";

            dgvTacGia.Columns[0].Width = 80;
            dgvTacGia.Columns[1].Width = 240;


            dgvTacGia.Columns[0].ReadOnly = true;
            dgvTacGia.MultiSelect = true;
            dgvTacGia.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvTacGia.DefaultCellStyle.SelectionBackColor = Color.Ivory; //Color.Silver;
            dgvTacGia.DefaultCellStyle.SelectionForeColor = Color.Black;

        }
        private void UpdateSoThuTuDataGridViewTacGia()
        {
            for (int i = 0; i < dgvTacGia.Rows.GetRowCount(DataGridViewElementStates.Visible) - 1; i++)
            {
                dgvTacGia.Rows[i].Cells[0].Value = i + 1;
            }
        }

        private void ThemTacGia_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewTacGia();
            if (dgvTacGia.Rows.Count - 1 == 0)
            {
                btnSave.Enabled = false;
                btnXoa.Enabled = false;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvTacGia_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
           
            AnHienButtonSave();
            AnHienButtonXoa();
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvTacGia.SelectedRows[0].IsNewRow==false)
            {
                dgvTacGia.Rows.Remove(dgvTacGia.SelectedRows[0]);
            }
        }

        private void dgvTacGia_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            MessageBox.Show("rowsremoved");
            AnHienButtonSave();
            AnHienButtonXoa();
        }

        private void dgvTacGia_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
           
            UpdateSoThuTuDataGridViewTacGia();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
       
           
                for (int i = 0; i < dgvTacGia.Rows.GetRowCount(DataGridViewElementStates.Visible) - 1; i++)
                {
                   
                    TacGiaDTO tacgiaDTO = new TacGiaDTO();
                    tacgiaDTO.TenTacGia = dgvTacGia.Rows[i].Cells[1].Value.ToString();
                    TacGiaService.Insert(tacgiaDTO);
                   
                    
                    
                    
                    MessageBox.Show(dgvTacGia.Rows[i].Cells[1].Value.ToString());
                }
                DialogResult r = MessageBox.Show("Tên Tác Giả Đã Lưu Thành Công", "Thông Báo", MessageBoxButtons.OK);
                if (r == DialogResult.OK)
                {
                    dgvTacGia.Rows.Clear();
                }
            
        }

        private void ThemTacGia_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btnSave.Enabled)
            {
                DialogResult r = MessageBox.Show("Bạn có muốn lưu dữ liệu không?", "Thông Báo", MessageBoxButtons.YesNo);
                if (r == DialogResult.Yes)
                {
                    btnSave_Click(sender, e);
                }
            }
        }

    }
}
